<script setup lang="ts">
import { ref, onMounted } from 'vue'
import useMsgUser from '@/components/index/message/hooks/msgUser'
const { userClicked, getUserList, msgItems, userList, isSelected } = useMsgUser()
const ul = ref<HTMLUListElement | null>(null)
onMounted(() => {
  getUserList()
})
</script>

<template>
  <ul ref="ul">
    <li
      v-for="item in userList"
      :key="item.id"
      :class="{ selected: isSelected === item.id }"
      @click="userClicked({ id: item.id, name: item.name, img: item.img })"
    >
      <div class="list-img">
        <img :src="item.img" alt="" />
      </div>
      <div>
        <span>
          <b>{{ item.name }}</b>
          <el-badge
            class="item"
            :value="msgItems[item.id]"
            :hidden="msgItems[item.id] === 0"
          ></el-badge>
        </span>
        <span>
          <p>{{ item.lastLog }}</p>
        </span>
      </div>
    </li>
  </ul>
</template>

<style scoped lang="less">
.selected {
  background-color: var(--color3);
  color: var(--color-font);
}
ul {
  display: block;
  height: 600px;
  width: 100%;
  overflow-y: scroll;
  overflow-x: hidden;
  scrollbar-width: none;
  background-color: var(--color1);
  border-radius: var(--radius);
  padding: 10px;
  box-sizing: border-box;
  > li:hover:not(.selected) {
    border: 2px solid var(--color3);
    color: var(--color3);
  }
  > li {
    cursor: pointer;
    list-style: none;
    display: flex;
    flex-wrap: nowrap;
    background-color: var(--color2);
    border-radius: var(--radius);
    margin: 6px 0;
    > .list-img {
      margin: 4px;
      > img {
        width: 42px;
        height: 42px;
      }
    }

    > div {
      height: 42px;
      width: 100%;
      align-self: center;
      position: relative;

      > span > .item {
        display: inline-block;
        vertical-align: middle;
      }

      > span b,
      > span p {
        width: calc(100% - 60px);
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      > span > b {
        display: inline-block;
        vertical-align: middle;
        height: 22px;
        line-height: 22px;
        font-size: 16px;
        width: 90px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      > span > p {
        font-size: 14px;
        width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
